package com.mjf.web.controller.ldpro;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mjf.cjt.domain.VoucherIncome;
import com.mjf.cjt.service.*;
import com.mjf.cjt.vo.*;
import com.mjf.common.utils.ResponseUtils;
import com.mjf.common.utils.ResponseVO;
import com.mjf.ldpro.domain.LdProjectInfo;
import com.mjf.ldpro.service.LdProjectInfoService;
import com.mjf.ldpro.vo.ExpenseParam;
import com.mjf.ldpro.vo.LdProjectInertVO;
import com.mjf.system.service.vo.XmsrmxVO;
import com.mjf.ys.service.YsProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/4/21 23:20
 */
@RestController
@RequestMapping("/ldcm/ldpro")
@Slf4j
public class LdProjectInfoController {

    @Autowired
    private LdProjectInfoService ldProjectInfoService;
    @Autowired
    private TProductReceivePushService productReceivePushService;

    @Autowired
    private TOtherInStockOrderService totherInStockOrderService;

    @Autowired
    private SaleDispatchService saleDispatchService;

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private TCjtExpensevoucherService tcjtExpensevoucherService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private TCjtzjbSaleDeliveryService tCjtzjbSaleDeliveryService;


    /**
     * 新增/更新项目信息
     * @param vo
     * @return
     */
    @PostMapping()
    public ResponseVO<Void> insertPro(@RequestBody LdProjectInertVO vo){
        ldProjectInfoService.insert(vo);
        return ResponseUtils.success();
    }

    /**
     * 三方接口
     * @return
     */
    @GetMapping("/open/list")
    public ResponseVO<Object> openList(){

        return ResponseUtils.success(ldProjectInfoService.openList());
    }

    /**
     * 删除项目信息
     * @return
     */
    @GetMapping("/test")
    public ResponseVO<Object> testM(){

        return ResponseUtils.success(ldProjectInfoService.testM());
    }

    @Autowired
    private YsProductService ysProductService;

    /**
     * 同步商品信息
     */
    @GetMapping("/test/product")
    public Object asyncProduct() {
        ysProductService.asyncProduct();
        return "success";
    }


    /**
     * 删除项目信息
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseVO<Void> delPro(@PathVariable("id") Long id){
        ldProjectInfoService.removeById(id);
        return ResponseUtils.success();
    }

    /**
     * 查询项目信息列表
     * @return
     */
    @GetMapping("/list")
    public ResponseVO<IPage<LdProjectInfo>> projectList(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        return ResponseUtils.success(ldProjectInfoService.projectList(page, size));
    }


    /**
     * 查询项目信息列表
     * @return
     */
    @PostMapping("/exp")
    public void textExp(HttpServletResponse response) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        List<XmsrmxVO> vos = new ArrayList<>();
        vos.add(XmsrmxVO.builder()
                .accountingperiod("2025-04-21")
                .code("0001")
                .departParentCode("0001")
                .departParentName("0001")
                .departCode("0001")
                .departName("0001")
            .build());
        // 写入到输出流
        EasyExcel.write(response.getOutputStream(), XmsrmxVO.class)
            .sheet("项目收入明细账")
            .doWrite(vos);
        outputStream.close();
    }

    /**
     * 查询T+费用单列表
     */
    @PostMapping("/expense/list")
    public ResponseVO<IPage<VoucherIncome>> expenseList(@RequestBody ExpenseParam param){
        return ResponseUtils.success(ldProjectInfoService.expenseList(param));
    }

    /**
     * 查询产品收货单
     */
    @PostMapping("/product/receive/list")
    public ResponseVO<IPage<ProductReceiveListVO>> queryProductReceive(@RequestBody ProductReceiveQueryParam param){
        return ResponseUtils.success(productReceivePushService.queryProductReceive(param));
    }

    /**
     * 查询产品收货单
     */
    @PostMapping("/product/receive/push")
    public ResponseVO<Void> queryProductReceive(@RequestBody List<String> ids){
        productReceivePushService.pushProductReceiveByIds(ids);
        return ResponseUtils.success();
    }

    /**
     * 查询其他入库
     */
    @PostMapping("/other/order/list")
    public ResponseVO<IPage<TOtherInStockOrderListVo>> queryOtherInStockOrder(@RequestBody TOtherInStockOrderQueryParam param){
        return ResponseUtils.success(totherInStockOrderService.queryOtherInStockOrder(param));
    }

    /**
     * 查询其他入库
     */
    @PostMapping("/other/order/push")
    public ResponseVO<Void> queryOtherInStockOrder(@RequestBody List<String> ids){
        totherInStockOrderService.otherInStockOrderByIds(ids);
        return ResponseUtils.success();
    }
    /**
     * 销售出库单列表同步
     */
    @PostMapping("/sale/dispatch")
    public ResponseVO<IPage<SaleDispatchListVo>> querySaleDispatch(@RequestBody SaleDispatchQueryParam param){

        return ResponseUtils.success(saleDispatchService.querySaleDispatch(param));
    }

    /**
     * 销售出库单列表查询
     */
    @PostMapping("/sale/dispatch/test")
    public void asyncSaleDispatchVoucher(@RequestParam("start") String start, @RequestParam("end")String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+销售出库单列表查询开始 {}", LocalDateTime.now());
            saleDispatchService.getSaleDispatchOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+销售出库单列表查询异常 ", e);
        }
    }

    /**
     * 销售订单列表同步
     */
    @PostMapping("/sale/Order")
    public ResponseVO<IPage<SaleOrderListVo>> querySaleOrder(@RequestBody SaleOrderQueryParam param){

        return ResponseUtils.success(saleOrderService.querySaleOrder(param));
    }

    /**
     * 销售订单列表同步
     */
    @PostMapping("/sale/Order/test")
    public void asyncSaleOrderVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+销售订单列表同步开始 {}", LocalDateTime.now());
            saleOrderService.getSaleOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+销售订单列表同步异常 ", e);
        }
    }

    /**
     * 分页费用单列表同步
     */
    @PostMapping("/Expensevoucher")
    public ResponseVO<IPage<TCjtExpensevoucherListVo>> queryTCjtExpensevoucher(@RequestBody TCjtExpensevoucherQueryParam param){

        return ResponseUtils.success(tcjtExpensevoucherService.queryTCjtExpensevoucher(param));
    }

    /**
     * 费用单列表同步
     */
    @PostMapping("/Expensevoucher/test")
    public void asyncExpensevoucherVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+费用单列表同步开始 {}", LocalDateTime.now());
            tcjtExpensevoucherService.getExpensevoucherFromRemote(start, end);
        }catch (Exception e){
            log.error("T+费用单列表同步异常 ", e);
        }
    }

    /**
     * 分页采购订单列表同步
     */
    @PostMapping("/PurchaseOrder")
    public ResponseVO<IPage<PurchaseOrderListVo>> queryPurchaseOrder(@RequestBody PurchaseOrderQueryParam param){

        return ResponseUtils.success(purchaseOrderService.queryPurchaseOrder(param));
    }

    /**
     * 采购订单列表同步
     */
    @PostMapping("/PurchaseOrder/test")
    public void asyncPurchaseOrderVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+采购订单列表同步开始 {}", LocalDateTime.now());
            purchaseOrderService.getPurchaseOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+采购订单列表同步异常 ", e);
        }
    }

    /**
     * 分页查询销货单
     */
    @PostMapping("/TCjtzjbSaleDelivery/list")
    public ResponseVO<IPage<TCjtzjbSaleDeliveryListVo>> queryTCjtzjbSaleDelivery(@RequestBody TCjtzjbSaleDeliveryQueryParam param){
        return ResponseUtils.success(tCjtzjbSaleDeliveryService.queryTCjtzjbSaleDelivery(param));
    }

    /**
     * 查询销货单
     */
    @PostMapping("/TCjtzjbSaleDelivery/push")
    public ResponseVO<Void> queryTCjtzjbSaleDelivery(@RequestBody List<String> ids){
        tCjtzjbSaleDeliveryService.tCjtzjbSaleDeliveryByIds(ids);
        return ResponseUtils.success();
    }
}
